//
//  ModifyPwdViewController.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/10.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "ModifyPwdViewController.h"

NSString const *ModifyPwdViewControllerTitleParamKey = @"ModifyPwdViewControllerTitleParamKey";

@interface ModifyPwdViewController ()<XUIGroupTableViewDelegate,XUIGroupTableViewDataSource,UITextFieldDelegate>
{
    XUIGroupTableView *_gtable;
    
    UITextField *_originTextFiled;
    UITextField *_newTextField;
    UITextField *_comfirmTextField;
    
    RSWDataOperation *_manager;
}
@end

@implementation ModifyPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self checkSession];
    
    [self setBackNavigateItemTitle:@"修改密码" rightItemText:@"提交"];
    
    self.view.backgroundColor = [UIColor colorWithHex:mcrmSAASUISkinColorTextStylef1f4f7Color];
    
    CGFloat top = [XUINavigationController navigationController].navigationBar.bottom;
    
    _gtable = [[XUIGroupTableView alloc]initWithFrame:CGRectMake(0, top, self.view.width, self.view.height)];
    _gtable.backgroundColor = [[UITableView appearance] backgroundColor];
    _gtable.delegate = self;
    _gtable.dataSource = self;
    
    [self.view addSubview:_gtable];
    
    [self addKeyboardObserver];
}

-(void)viewControllerWillDealloc
{
    [super viewControllerWillDealloc];
    [self removeObserver];
    [_manager terminateRequest];
}

#pragma mark --
#pragma mark -- XUIGroupTableViewDelegate XUIGroupTableViewDataSource
- (NSUInteger)numberOfGroupsInGroupTableView:(XUIGroupTableView *)groupTableView
{
    return 1;
}

- (NSUInteger)groupTableView:(XUIGroupTableView *)groupTableView numberOfRowsInGroup:(NSInteger)group
{
    return 3;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (CGFloat)groupTableView:(XUIGroupTableView *)groupTableView heightForHeaderInGroup:(NSInteger)group
{
    return 10;
}

- (XUIGroupTableViewCell *)groupTableView:(XUIGroupTableView *)groupTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XUIGroupTableViewCell *cell = [[XUIGroupTableViewCell alloc] initWithFrame:CGRectZero];
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [self groupTableView:nil heightForRowAtIndexPath:indexPath];
    
    CGSize s = [NSString charSizeWithFont:[UIFont systemFontOfSize:16]];
    
    UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(10, (height - s.height)/2, s.width * 4, s.height)];
    l.backgroundColor = [UIColor clearColor];
    l.font = [UIFont systemFontOfSize:16];
    l.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:l];
    
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(l.right + 10, (height - 40)/2, groupTableView.width - 10 - l.width - 10 - 10 , 40)];
    textField.delegate = self;
    textField.font = [UIFont systemFontOfSize:16];
    textField.returnKeyType = UIReturnKeyNext;
    textField.secureTextEntry = YES;
    [cell.contentView addSubview:textField];
    
    switch(indexPath.row)
    {
        case 0:
        {
            l.text = @"旧的密码";
            textField.placeholder = @"请输入您的旧的密码";
         
            _originTextFiled = textField;
            
            break;
        }
        case 1:
        {
            l.text = @"新的密码";
            textField.placeholder = @"请输入新的密码";
            
            _newTextField = textField;
            
            break;
        }
        case 2:
        {
            l.text = @"确认密码";
            textField.placeholder = @"请输入确认密码";
            
            textField.returnKeyType = UIReturnKeyDone;
            
            _comfirmTextField = textField;
            
            break;
        }
    }
    
    return cell;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField == _originTextFiled)
    {
        [_newTextField becomeFirstResponder];
    }
    else if(textField == _newTextField)
    {
        [_comfirmTextField becomeFirstResponder];
    }
    else
    {
        //[self submitPassword];
    }
    return YES;
}

-(void)clickedBackBarButton:(id)sender
{
    [_manager terminateRequest];
    
    [XUINavigationController pop];
}

-(void)clickedRightBarButton:(id)sender
{
    [self submitPassword];
}

-(void)beginProgressView:(XMSDataOperationManager *)dom
{
    [self beginProgressViewPanel:@"提交中，请稍候"];
}

-(void)submitPassword
{
    if(_originTextFiled.text.length <= 0)
    {
        [UIAlertView showInfoMsg:@"请输入旧的密码"];
        return;
    }
    if(_newTextField.text.length <= 0)
    {
        [UIAlertView showInfoMsg:@"请输入新的密码"];
        return;
    }
    if([_newTextField.text isEqualToString:_comfirmTextField.text] != YES)
    {
        [UIAlertView showInfoMsg:@"两次填写的密码不一致"];
        return;
    }
    
    [UIViewController closeKeyBoard];
    
    if(_manager == nil)
        _manager = [[RSWDataOperation alloc]initWithDataOperationManagerDelegate:self];
    
    [_manager modifyPasswordWithOldPassword:_originTextFiled.text newPassword:_newTextField.text completion:^(NSInteger nResult, BOOL bResult, NSError *error)
     {
         switch(nResult)
         {
             case YES:
             {
                 if(bResult == YES)
                 {
                     [XUIAlertView alertViewWithTitle:@"提示" message:@"密码修改成功" cancelButtonTitle:@"确定" otherButtonTitles:nil clickBlock:^(NSInteger nClickindex)
                      {
                          [XUINavigationController pop];
                      }];
                 }
                 else
                 {
                     [UIAlertView showErrorMsg:@"密码修改失败"];
                 }
                 break;
             }
             default:
             {
                 [UIAlertView showErrorMsg:@"密码修改失败"];
                 break;
             }
         }
     }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
